//
//  ForgetPwdViewModel.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/1.
//

import Foundation
import Combine

class ForgetPwdViewModel: ObservableObject {
    @Published var account: String = ""
    @Published var showToast = false
    @Published var toastMsg = ""
    @Published var sendSuccess = false
    @Published var isLoading = false
    
    var cancellables = Set<AnyCancellable>()
    
    init() {
        toastMsg = ""
        sendSuccess = false
        showToast = false
        account = ""
    }
    
    var isAccountValid: Bool {
        account.isEmail || account.isPhoneNum
    }
    
    func requestVerifyCode()
    {
        if isAccountValid == false {
            return;
        }
        UIApplication.shared.hideKeyboard()
        
        CDGetVerifyCodeStep().execute(.init(account: account, type: .notRegister))
            .receive(on: DispatchQueue.main)
            .sink(receiveCompletion: {[weak self] completion in
                switch completion {
                case .finished:
                    print("119615, 获取验证码 finished")
                    self?.isLoading = false
                case .failure(let error):
                    print("119615, 获取验证码 error: \(error)")
                    self?.showToast = true
                    self?.isLoading = false
                    self?.toastMsg = error.localizedDescription
                }
            }, receiveValue: {[weak self] userInfo in
                print("119615, 获取验证码 value: \(userInfo)")
                self?.isLoading = false
                self?.sendSuccess = true
            })
            .store(in: &cancellables)
        
        UIApplication.shared.hideKeyboard()
        
    }
}
    
